package com.springboot2.shiro.web.handler;

import com.springboot2.shiro.comm.entity.RespResult;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;

/**
 * DefaultExceptionHandler
 *
 * @author Archx[archx@foxmail.com]
 * at 2018/7/13 22:50
 */
@ControllerAdvice(annotations = RestController.class)
public class DefaultExceptionHandler {

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public RespResult handleUnauthorizedException(NativeWebRequest request, Object handler, UnauthorizedException ex) {
        return RespResult.builder().code(RespResult.Code.Exception).message(ex.getMessage()).build();
    }
}
